package com.shop.coupon;

import com.shop.coupon.domain.vo.CouponHistoryVo;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestBatch {
    private static int threadCount = 30;
    private final static CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(threadCount);
    private static final RestTemplate restTemplate = new RestTemplate();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);

        for (int i = 0; i < threadCount; i++) {
            executorService.submit(() -> {
                try {
                    COUNT_DOWN_LATCH.countDown();
                    COUNT_DOWN_LATCH.await();

                    for (int j = 1; j <= 30; j++) {
                        int param = new Random().nextInt(4) + 1; // 生成1到4的随机数
                        String url = "http://localhost:9222/couponHistoryVo/BathCouponVOList";
                        CouponHistoryVo couponHistoryVo = new CouponHistoryVo(); // 构造请求参数对象
                        // 设置 couponHistoryVo 的属性，根据实际情况设置

                        // 发送 POST 请求
                        List<CouponHistoryVo> response = restTemplate.postForObject(url, couponHistoryVo, List.class);
                        System.out.println(Thread.currentThread().getName() + " 参数 " + param + " 返回值 " + response);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executorService.shutdown();
    }
}
